<?php


include '../settings.php'; 
$con=mysqli_connect($host,$dbuserW,$dbpassW,$dbname);


if (!empty($_FILES))
{
//    echo "<pre>";
//    print_r($_FILES);
//    echo file_get_contents($_FILES['file']['tmp_name']);


$handle = fopen($_FILES['file']['tmp_name'], "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        //echo $line;
        $line = rtrim($line, "\r\n");
        $pieces = explode("|", $line);
        
        if($pieces[0]=="Catagory") 
        {
        //echo "Category\n";
        $sql="Insert into Category Value ('".$pieces[1]."','".$pieces[2]."','','','0')";
        echo $sql."<br>";
        $res=mysqli_query($con,$sql);
        $errors=mysqli_error($con);
        echo("<br>Errorno=".$errors."<br>");
        if (mysqli_errno($con)) 
        {
	  echo("-------Duplicate entry means you already have this piece of data do not worry -----\n<br>");
	  $Cat="";
	  $numwrong=$numwrong+1;
	}
	else $numright=$numright+1;
        }
        
        if($pieces[0]=="CategoryAya") 
        {
        //echo "CategoryAya\n";
        $sql="Insert into CategoryAya Value ('".$pieces[1]."','".$pieces[2]."','".$pieces[3]."','".$pieces[4]."','0')";
        echo $sql."\n";
        echo $sql."<br>";
        $res=mysqli_query($con,$sql);
        $errors=mysqli_error($con);
        echo("<br>Errorno=".$errors."<br>");
        if (mysqli_errno($con)) 
        {
	  echo("-------Duplicate entry means you already have this piece of data do not worry -----\n<br>");
	  $Cat="";
	  $numwrong=$numwrong+1;
	}
	else $numright=$numright+1;
	
        }
	

    }
    echo "<br>Correct Imports=".$numright."<br>";
    echo "<br>Duplicate or corrupt Imports=".$numwrong."<br>";
} else {
    // error opening the file.
} 
fclose($handle);

    echo "</pre>";
}
?>
<form id="myForm" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" value="Send" />
</form>